package com.deer.wms.ASN.manage.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.deer.wms.ASN.manage.model.asn.AsnMaster;
import com.deer.wms.ASN.manage.model.file.FileData;
import com.deer.wms.ASN.manage.model.in.AcceptRecord;
import com.deer.wms.ASN.manage.model.in.AcceptRecordCriteria;
import com.deer.wms.ASN.manage.model.qc.*;
import com.deer.wms.ASN.manage.service.AcceptRecordService;
import com.deer.wms.ASN.manage.service.AsnMasterService;
import com.deer.wms.ASN.manage.service.QcRecordService;
import com.deer.wms.intercept.annotation.NoRepeatSubmit;
import com.deer.wms.intercept.annotation.User;
import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.project.root.core.result.CommonCode;
import com.deer.wms.project.root.exception.ServiceException;
import com.deer.wms.project.root.util.DateUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.deer.wms.project.root.core.result.Result;
import com.deer.wms.project.root.core.result.ResultGenerator;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.concurrent.CompletableFuture;

import static com.deer.wms.project.root.util.PageUtils.getStart;

/**
*
 * @author guo
 * @since 2020/01/15
 */
@Api(description = "质检记录api接口")
@RestController
@RequestMapping("/qc/records")
public class QcRecordController {

    @Autowired
    private QcRecordService qcRecordService;
    @Autowired
    private AcceptRecordService acceptRecordService;
    @Autowired
    private AsnMasterService asnMasterService;

    @PostMapping("/insert")
    public Result add(@RequestBody QcRecord qcRecord) {
        qcRecordService.save(qcRecord);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/delete")
    public Result delete(Integer id) {
        qcRecordService.removeById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody QcRecord qcRecord) {
        qcRecordService.updateById(qcRecord);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/detail")
    public Result detail(@PathVariable Integer id) {
        QcRecord qcRecord = qcRecordService.getById(id);
        return ResultGenerator.genSuccessResult(qcRecord);
    }

    @GetMapping("/list")
    public Result list(QcRecordCriteria criteria) {
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<QcRecordDto> list = qcRecordService.findList(criteria);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }


    @GetMapping("/return/list")
    public Result returnList(QcRecordCriteria criteria) {
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<QcRecordDto> list = qcRecordService.findReturnList(criteria);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }


    @ApiOperation(value = "退料确认")
    @GetMapping("/allotPrint")
    public Result allotPrint( String billNo, @ApiIgnore @User CurrentUser currentUser) {
        return ResultGenerator.genSuccessResult(qcRecordService.allotPrint(billNo,currentUser));
    }


//    // 质检任务池的进行质检结果确认
//    @GetMapping("result")
//    @Transactional
//    public Result qcResult(QcResultCriteria criteria, @ApiIgnore @User CurrentUser currentUser) {
//        qcRecordService.qcResult(criteria,currentUser);
//        //回传erp
//        AcceptRecord acceptRecord = acceptRecordService.getOneByField("accept_record_code", criteria.getBillCode());
////        String billNo = acceptRecord.getAsnBillNo();
////        QcRecord qcRecord = qcRecordService.getOneByField("accept_record_code", criteria.getBillCode());
//        String billId = acceptRecordService.billToErpNew(acceptRecord.getAsnBillNo(), newQcRecord);
//        //erp审核回传
//         acceptRecordService.checkBillToErp(billId);
//
//        return ResultGenerator.genSuccessResult();
//    }

    // 批量质检提交
    @PostMapping("/submit")
    @NoRepeatSubmit
    public Result submit(@RequestBody QcResultCriteria criteria, @ApiIgnore @User CurrentUser currentUser) {

        if(criteria.getQcRecordList()!=null){
            List<QcRecord> qcRecordList = criteria.getQcRecordList();
            for (QcRecord qcRecord : qcRecordList) {
                qcRecordService.submit(qcRecord, currentUser);
            }
        }else {
            //全部合格
            AcceptRecordCriteria acceptRecordCriteria = new AcceptRecordCriteria();
            acceptRecordCriteria.setAsnBillNo(criteria.getBillCode());
            acceptRecordCriteria.setPageNum(0);
//            criteria.setPageNum(getStart(criteria));
            List<AcceptRecord> list = acceptRecordService.findList(acceptRecordCriteria);
//            List<AcceptRecord> list = acceptRecordService.findList(acceptRecordCriteria);
            for (AcceptRecord acceptRecord : list) {
                if(acceptRecord.getQcState()!=2){
                    qcRecordService.submitAll(acceptRecord, currentUser);

                }
            }
        }
        return ResultGenerator.genSuccessResult();
    }

    @ApiOperation(value = "归档质检记录 传billNo", notes = "归档质检记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class,  required = true)
    })
    @PostMapping("/file")
    public Result file(@RequestBody List<QcRecord> qcRecordList, @ApiIgnore @User CurrentUser currentUser) {
        if(currentUser == null){
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }
        for (QcRecord qcRecord : qcRecordList) {
                acceptRecordService.checkBillToErp(qcRecord.getAuditId());
                qcRecord.setAuditState(1);
                qcRecordService.updateById(qcRecord);
        }


        return ResultGenerator.genSuccessResult();
    }

}
